package com.ticket.sass.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.common.entity.tenant.ServiceChargeEntity;
import com.ticket.sass.provider.mapper.ServiceChargeMapper;
import com.ticket.sass.provider.service.BaseService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ywb
 * @since 2024-03-22
 */
@Service
public class ServiceChargeServiceImpl extends ServiceImpl<ServiceChargeMapper, ServiceChargeEntity> implements BaseService<ServiceChargeEntity> {
    /**
     * 根据影院ID列表查询服务收费信息，并返回一个包含影院ID作为键，ServiceChargeEntity实体作为值的Map。
     *
     * @param cinemaIds 影院ID列表
     * @return 包含影院ID作为键，ServiceChargeEntity实体作为值的Map
     */
    public Map<Long, ServiceChargeEntity> getServiceChargesByCinemaIds(List<Long> cinemaIds) {
        if (cinemaIds.size() == 0){
            return new HashMap<>();
        }

        // 创建查询条件，筛选 cinemaId 在列表中的记录
        QueryWrapper<ServiceChargeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("cinema_id", cinemaIds);
        
        // 查询符合条件的所有记录
        List<ServiceChargeEntity> serviceChargeEntities = list(queryWrapper);
        
        // 将查询结果转换为 Map<cinemaId, ServiceChargeEntity>
        return serviceChargeEntities.stream()
            .collect(Collectors.toMap(ServiceChargeEntity::getCinemaId, serviceChargeEntity -> serviceChargeEntity));
    }
    
    /**
     * 根据影院ID更新 channelServiceFee 字段
     *
     * @param cinemaId 影院ID
     * @param newChannelServiceFee 新的渠道服务费
     * @return 更新结果，成功返回 true，失败返回 false
     */
    public boolean updateChannelServiceFeeByCinemaId(Long cinemaId, BigDecimal newChannelServiceFee) {
        // 创建更新条件，设置需要更新的字段
        UpdateWrapper<ServiceChargeEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("cinema_id", cinemaId)
            .set("channel_service_fee", newChannelServiceFee);
        
        // 执行更新操作
        return update(updateWrapper);
    }
}
